package com.zhdl.hardware.modules.service.impl;

import com.project.common.bean.BaseQueryCriteria;
import com.project.common.service.impl.BaseServiceImpl;
import com.project.network.entity.PortComm;
import com.project.network.entity.PortSocket;
import com.project.network.service.PortCommService;
import com.project.network.service.PortSocketService;
import com.zhdl.hardware.io.smartLink.repository.impl.IOCmdHandleDaoImpl;
import com.zhdl.hardware.modules.entity.DeviceInfo;
import com.zhdl.hardware.modules.entity.DeviceProcess;
import com.zhdl.hardware.modules.entity.ProcessStep;
import com.zhdl.hardware.modules.entity.ProcessSystem;
import com.zhdl.hardware.modules.repository.DeviceProcessRepository;
import com.zhdl.hardware.modules.repository.DeviceRepository;
import com.zhdl.hardware.modules.repository.ProcessStepRepository;
import com.zhdl.hardware.modules.service.DeviceService;
import com.zhdl.hardware.modules.web.response.DeviceInfoResp;
import com.zhdl.hardware.modules.web.response.ProcessStepResp;
import com.zhdl.hardware.modules.web.response.ProcessSystemResp;
import com.zhdl.hardware.motor.huichuan.repository.HcPortCmdHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class DeviceServiceImpl extends BaseServiceImpl<String, DeviceInfo, DeviceRepository> implements DeviceService {

    @Resource
    PortCommService portCommService;

    @Resource
    PortSocketService portSocketService;

    @Resource
    DeviceRepository deviceRepository;

    @Resource
    DeviceProcessRepository deviceProcessRepository;

    @Resource
    HcPortCmdHandle hcPortCmdHandle;
    @Resource
    IOCmdHandleDaoImpl ioCmdHandleDao;
    @Resource
    ProcessStepRepository processStepRepository;

    /**
     * 初始化init
     */
    @Override
    public void hardwareInit() {
        List<DeviceInfoResp> allDevice = findAllDevice();
        for (DeviceInfoResp deviceInfoResp : allDevice) {
            if (deviceInfoResp.getTypeId() == 0) {
                hcPortCmdHandle.addPort(deviceInfoResp.getDeviceProcess().getPort());
            } else if (deviceInfoResp.getTypeId() == 1) {
//                bukePortCmdHandle.addPort(deviceInfoResp.getDeviceProcess().getPort());
            } else if (deviceInfoResp.getTypeId() == 2) {
                ioCmdHandleDao.init(deviceInfoResp.getDeviceProcess().getPrefix()
                        + ":" + deviceInfoResp.getDeviceProcess().getPort());
            }
        }
        processInit();
    }

    /**
     * 初始化流程
     */
    public void processInit() {
//        filterProcess.init();
    }

    /**
     * 配置设备信息
     */
    @Override
    public boolean setDeviceInfo() {
        BaseQueryCriteria bqc = new BaseQueryCriteria();
        List<PortComm> listCom = portCommService.findList(bqc);
        List<PortSocket> listSocket = portSocketService.findList(bqc);

        for (PortComm portComm : listCom) {
            DeviceProcess deviceProcess = new DeviceProcess();
            deviceProcess.setType("rtu");
            deviceProcess.setPrefix("com");
            deviceProcess.setPort(portComm.getPort());
            deviceProcess.setBaudRate(portComm.getBaudRate());
            deviceProcessRepository.save(deviceProcess);
        }
        for (PortSocket portSocket : listSocket) {
            DeviceProcess deviceProcess = new DeviceProcess();
            deviceProcess.setType("tcp");
            deviceProcess.setPrefix(portSocket.getServerIp());
            deviceProcess.setPort(portSocket.getServerPort());
            deviceProcessRepository.save(deviceProcess);
        }
        return true;
    }

    @Override
    public ProcessSystemResp findAllSystemStep(int id) {

        ProcessSystem processSystem = deviceRepository.findSystemByProcessId(id);
        List<ProcessStep> processSteps = deviceRepository.findStepByProcessId(id);

        ProcessSystemResp processSystemResp = new ProcessSystemResp();
        List<ProcessStepResp> processStepResps = new ArrayList<>();

        processSystemResp.setStepCount(processSteps.size());
        processSystemResp.setSystemRemark(processSystem.getProcessRemark());

        for (ProcessStep processStep : processSteps) {
            ProcessStepResp processStepResp = new ProcessStepResp();
            processStepResp.setStepId(processStep.getStepId());
            processStepResp.setStepNum(processStep.getStepNum());
            processStepResp.setStepParam(processStep.getStepParam());
            processStepResp.setStepRemark(processStep.getRemark());
            processStepResp.setActionType(processStep.getActionType());
            processStepResps.add(processStepResp);
        }

        processSystemResp.setStepRespList(processStepResps);

        return processSystemResp;
    }

    /**
     * 修改参数
     */
    @Override
    public void setStepParam(int id, String param) {
        Optional<ProcessStep> byId = processStepRepository.findById(id);
        ProcessStep processStep = byId.get();
        processStep.setStepParam(param);
        processStepRepository.save(processStep);
    }

    /**
     * 返回设备详情
     *
     * @return 设备参数详情
     */
    @Override
    public List<DeviceInfoResp> findAllDevice() {
        List<DeviceProcess> deviceProcesses = deviceProcessRepository.findAll();
        List<DeviceInfo> deviceInfos = deviceRepository.findDeviceInfoById();
        List<DeviceInfoResp> deviceInfoResps = new ArrayList<>();

        for (DeviceProcess deviceProcess : deviceProcesses) {
            DeviceInfoResp deviceInfoResp = new DeviceInfoResp();
            deviceInfoResp.setDeviceProcess(deviceProcess);
            for (DeviceInfo deviceInfo : deviceInfos) {
                if (deviceProcess.getDeviceId() == deviceInfo.getDeviceId()) {
                    deviceInfoResp.setId(deviceInfo.getDeviceId());
                    deviceInfoResp.setName(deviceInfo.getDeviceName());
                    deviceInfoResp.setTypeId(deviceInfo.getTypeId());
                    deviceInfoResps.add(deviceInfoResp);
                }
            }
        }
        return deviceInfoResps;
    }

}

//        for (DeviceInfo deviceInfo : deviceInfos) {
//            DeviceInfoResp deviceInfoResp = new DeviceInfoResp();
//            deviceInfoResp.setId(deviceInfo.getMId());
//            deviceInfoResp.setName(deviceInfo.getDeviceName());
//            deviceInfoResp.setTypeId(deviceInfo.getTypeId());
//            for (DeviceProcess deviceProcess : deviceProcesses) {
//                if (deviceInfoResp.getId() == deviceProcess.getMId()){
//                    deviceInfoResp.setDeviceProcess(deviceProcess);
//                    deviceInfoResps.add(deviceInfoResp);
//                }
//            }
//            log.info(deviceInfo.toString());
//        }


//        for (DeviceInfo deviceInfo : deviceInfos) {
//            DeviceInfoResp deviceInfoResp = new DeviceInfoResp();
//            deviceInfoResp.setId(deviceInfo.getDeviceId());
//            deviceInfoResp.setName(deviceInfo.getDeviceName());
//            deviceProcessRepository.findById(mId).ifPresent(d -> deviceInfoResp.setDeviceProcess(d));
//            deviceInfoResps.add(deviceInfoResp);
//        }